﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class customerController
    {

        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static customerInfo getbyID(int customerID)
        {
            customerInfo objCustomer = new customerInfo();
            using (SqlCommand cmd = new SqlCommand("getIDCustomer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@customerID", customerID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objCustomer.customerID = dr.GetInt32(0);
                        objCustomer.customerName = dr.GetString(1);
                        objCustomer.email = dr.GetString(2);
                        objCustomer.password = dr.GetString(3);
                        objCustomer.address = dr.GetString(4);
                        objCustomer.phone = dr.GetString(5);
                    }
                }
                dr.Close();
            }
            return objCustomer;
        }

        #endregion

        #region getbyAll
        public static DataTable ListCustomer()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listCustomer", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertCustomer(customerInfo objCustomer)
        {
            try
            {

                 using (SqlCommand cmd = new SqlCommand("insertCustomer", con))
            
                 {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@customerName", objCustomer.customerName));
                cmd.Parameters.Add(new SqlParameter("@email", objCustomer.email));
                cmd.Parameters.Add(new SqlParameter("@password", objCustomer.password));
                cmd.Parameters.Add(new SqlParameter("@address", objCustomer.address));
                cmd.Parameters.Add(new SqlParameter("@phone", objCustomer.phone));
                cmd.ExecuteNonQuery();

             
                return true;
                 }

            }
            catch (Exception)
            {
                return false;
            }
        }
        
        #endregion

        #region update
        public static bool updateCustomer(customerInfo objCustomer)
        {
            using (SqlCommand cmd = new SqlCommand("updateAuthor", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@customerName", objCustomer.customerName));
                cmd.Parameters.Add(new SqlParameter("@email", objCustomer.email));
                cmd.Parameters.Add(new SqlParameter("@password", objCustomer.password));
                cmd.Parameters.Add(new SqlParameter("@address", objCustomer.address));
                cmd.Parameters.Add(new SqlParameter("@phone", objCustomer.phone));
                cmd.ExecuteNonQuery();
                return true;
            }
            
        }
        #endregion

        #region delete
        public static bool deleteCustomer(int customerID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteCustomer", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@customerID", customerID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
        public bool CheckUser_customer(customerInfo cus)
        {

            using (SqlCommand command = new SqlCommand("checkcustomer", con))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@email", cus.email));
                command.Parameters.Add(new SqlParameter("@pass", cus.password));
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public bool CheckUser() 
        {
            using (SqlCommand cmd = new SqlCommand("checkuser",con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.Add(new SqlParameter("@email",email));
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                dt = new DataTable();
                adapter.Fill(dt);
                return true;
            }
            
        }
    }
}
